# RUN ANALYSES IN "IE analyses.do" to verify results!!!

# Figure A2
## Monarchism
fa2data.m <- data.frame(period=c(1980,1990,1995,2000,2005,2010,2015,2020),
                        effect=c(.1353603,-.0382868,-.0356861,-.0465837,-.027333,.0420488,.0187525,-.048272),
                        se=c(.0093615,.0093986,.0051289,.0042129,.0046199,.0065945,.0067621,.0085213))
fa2data.m$lci <- fa2data.m$effect-(1.96*fa2data.m$se)
fa2data.m$uci <- fa2data.m$effect+(1.96*fa2data.m$se)
fa2data.m$sig <- factor(ifelse(fa2data.m$lci<0 & fa2data.m$uci>0,"No","Yes"))
## Abolition
fa2data.a <- data.frame(period=c(1980,1990,1995,2000,2005,2010,2015,2020),
                        effect=c(-.0484308,.0184171,.0211062,.0150777,.0002342,-.0273532,-.0220149,.0429638),
                        se=c(.0087603,.008795,.0047995,.0039423,.0043231,.006171,.0063278,.007974))
fa2data.a$lci <- fa2data.a$effect-(1.96*fa2data.a$se)
fa2data.a$uci <- fa2data.a$effect+(1.96*fa2data.a$se)
fa2data.a$sig <- factor(ifelse(fa2data.a$lci<0 & fa2data.a$uci>0,"No","Yes"))
## Joint Figure
fa2.m <- ggplot(fa2data.m,aes(x=period,y=effect)) + geom_point() + theme_classic() + geom_errorbar(aes(ymin=lci,ymax=uci),lwd=0.5,width=0) + geom_hline(yintercept=0,linetype="dashed",color="red") +xlab("Period") + ylab("Difference from Grand Mean") + ggtitle("Period, Monarchism") + theme(plot.title = element_text(hjust = 0.5))
fa2.a <- ggplot(fa2data.a,aes(x=period,y=effect,color=sig)) + geom_point() + theme_classic() + geom_errorbar(aes(ymin=lci,ymax=uci),lwd=0.5,width=0) + geom_hline(yintercept=0,linetype="dashed",color="red") +xlab("Period") + ylab("Difference from Grand Mean") + scale_color_manual(values=c("Grey50","Black"),guide="none") + ggtitle("Period, Abolition") + theme(plot.title = element_text(hjust = 0.5))
fa2 <- ggpubr::ggarrange(fa2.m,fa2.a,nrow=1)
# ggsave("Figure A2.png",fa2,device="png",width=6,height=4,units="in")

# Figure A3
## Monarchism
fa3data.m <- data.frame(age=c(15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90),
                       effect=c(-.0764786,-.0669484,-.0703861,-.0627681,-.048188,-.0325255,-.0327893,-.0099891,.0049142,.0087833,.0276435,.0346443,.046474,.0736181,.1122432,.0917526),
                       se=c(.01915157,.0150215,.0128243,.0108201,.0092325,.0079878,.0072569,.0068902,.0071427,.0077949,.0089746,.0105071,.0125021,.0147528,.0187003,.0269173))
fa3data.m$lci <- fa3data.m$effect-(1.96*fa3data.m$se)
fa3data.m$uci <- fa3data.m$effect+(1.96*fa3data.m$se)
fa3data.m$sig <- factor(ifelse(fa3data.m$lci<0 & fa3data.m$uci>0,"No","Yes"))
## Abolition
fa3data.a <- data.frame(age=c(15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90),
                       effect=c(.0247025,.0279809,.0281063,.0214944,.0222369,.0158319,.0145001,-.0000118,-.0029634,.0105789,-.0079847,-.0160144,-.0126858,-.0261819,-.0479407,-.0516494),
                       se=c(.0182623,.0140567,.0120006,.0101251,.0086395,.0074747,.0067909,.0064477,.006684,.0072942,.0083982,.0098322,.0116991,.0138053,.0174992,.0251885))
fa3data.a$lci <- fa3data.a$effect-(1.96*fa3data.a$se)
fa3data.a$uci <- fa3data.a$effect+(1.96*fa3data.a$se)
fa3data.a$sig <- factor(ifelse(fa3data.a$lci<0 & fa3data.a$uci>0,"No","Yes"))
## Joint Figure
fa3.m <- ggplot(fa3data.m,aes(x=age,y=effect,color=sig)) + geom_point() + theme_classic() + geom_errorbar(aes(ymin=lci,ymax=uci),lwd=0.5,width=0) + geom_hline(yintercept=0,linetype="dashed",color="red") +xlab("Age") + ylab("Difference from Grand Mean") + scale_color_manual(values=c("Grey50","Black"),guide="none") + ggtitle("Age, Monarchism") + theme(plot.title = element_text(hjust = 0.5))
fa3.a <- ggplot(fa3data.a,aes(x=age,y=effect,color=sig)) + geom_point() + theme_classic() + geom_errorbar(aes(ymin=lci,ymax=uci),lwd=0.5,width=0) + geom_hline(yintercept=0,linetype="dashed",color="red") +xlab("Age") + ylab("Difference from Grand Mean") + scale_color_manual(values=c("Grey50","Black"),guide="none") + ggtitle("Age, Abolition") + theme(plot.title = element_text(hjust = 0.5))
fa3 <- ggpubr::ggarrange(fa3.m,fa3.a,nrow=1)
# ggsave("Figure A3.png",fa3,device="png",width=6,height=4,units="in")

# Figure A4
## Monarchism
fa4data.m <- data.frame(cohort=seq(1890,2000,by=5),
                       effect=c(.0349484,.0112252,-.0061687,.0273683,.039557,.0263542,.0277451,.0233519,.047465,.0354685,.039539,.0104092,.016926,.006868,.0060249,.0036146,-.015964,-.0059906,-.0050664,-.035592,-.0908989,-.096603,-.1005816),
                       se=c(.182987,.0841173,.0406312,.0320418,.0278455,.0248828,.0224301,.0204398,.0185211,.0165265,.0144807,.0126477,.0110234,.0093909,.0079262,.0071473,.007403,.0081711,.0098204,.0120309,.0151904,.0204893,.0429276))
fa4data.m$lci <- fa4data.m$effect-(1.96*fa4data.m$se)
fa4data.m$uci <- fa4data.m$effect+(1.96*fa4data.m$se)
fa4data.m$sig <- ifelse(fa4data.m$lci<0 & fa4data.m$uci>0,"No","Yes")
## Abolition
fa4data.a <- data.frame(cohort=seq(1890,2000,by=5),
                       effect=c(.0269337,-.009709,.0062378,-.0133348,-.0240068,-.0095047,-.0152044,-.014642,-.0274006,-.0127972,-.0186087,-.0084943,-.0117838,-.0086908,-.0103529,-.006836,.0061613,-.0021444,-.0091418,.006599,.0685699,.0446525,.0434981),
                       se=c(.1712344,.0787148,.0380216,.0299838,.0260571,.0232847,.0209895,.019127,.0173315,.015465,.0135506,.0118354,.0103154,.0087878,.0074171,.0066883,.0069275,.0076463,.0091897,.0112582,.0142148,.0191733,.0401705))
fa4data.a$lci <- fa4data.a$effect-(1.96*fa4data.a$se)
fa4data.a$uci <- fa4data.a$effect+(1.96*fa4data.a$se)
fa4data.a$sig <- ifelse(fa4data.a$lci<0 & fa4data.a$uci>0,"No","Yes")
## Joint Figure
fa4.m <- ggplot(fa4data.m,aes(x=cohort,y=effect,color=sig)) + geom_point() + theme_classic() + geom_errorbar(aes(ymin=lci,ymax=uci),lwd=0.5,width=0) + geom_hline(yintercept=0,linetype="dashed",color="red") +xlab("Cohort") + ylab("Difference from Grand Mean") + scale_color_manual(values=c("Grey50","Black"),guide="none") + ggtitle("Cohort, Monarchism") + theme(plot.title = element_text(hjust = 0.5))
fa4.a <- ggplot(fa4data.a,aes(x=cohort,y=effect,color=sig)) + geom_point() + theme_classic() + geom_errorbar(aes(ymin=lci,ymax=uci),lwd=0.5,width=0) + geom_hline(yintercept=0,linetype="dashed",color="red") +xlab("Cohort") + ylab("Difference from Grand Mean") + scale_color_manual(values=c("Grey50","Black"),guide="none") + ggtitle("Cohort, Abolition") + theme(plot.title = element_text(hjust = 0.5))
fa4 <- ggpubr::ggarrange(fa4.m,fa4.a,nrow=1)
# ggsave("Figure A4.png",fa4,device="png",width=6,height=4,units="in")
